﻿/**
 * @author Colin
 */
class com.mixmedia.utils.StringUtils {
	public static function thousandSeperator(num:Number):String{
		var rstring:String = String(num);
		var sa:Array = rstring.split(".");
		//add separator in integer or only integer 
		var a:Array = sa[0].split("");
		if(a.length>3){//add seperator to every 4 digit
			a.reverse();
			for(var i=3;i<a.length;i+=4){a.splice(i,0,",");}
			a.reverse();
		}
		rstring = a.join("");
		return rstring;
	}
	
	public static function timeToString(time:Number):String{
		if(isNaN(time))time=0;
		var sec:Number = Math.floor(time%60);
		var min:Number = Math.floor(time/60)%60;
		var hrs:Number = Math.floor(time/60/60);

		var secStr:String = (sec>=10)?String(sec):"0"+sec;
		var minStr:String = (min>=10)?String(min):"0"+min;
		var hrsStr:String = (hrs<=0)?"":String(hrs)+":";
		
		return hrsStr+minStr+":"+secStr;
	}
	
	public static function safeUnescape(str:String) {
		while (str.indexOf("%") != 0 && str != unescape(str) ) {
			str = unescape(str);
		}
		return str;
	}

	public static function replace(str:String, oldStr:String, newStr:String) {
		var tmpStr:String = str;
		var ary = tmpStr.split(oldStr);
		tmpStr = ary.join(newStr);
		return tmpStr;
	}
}